<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改管理员信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">
    <script type="text/javascript" src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/Sys/js/xadmin.js"></script>
</head>

<body>
    <div class="layui-container" style="padding: 20px;">
        <form class="layui-form" action="">
            <input type="hidden" name="id" id="userId">
            
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required lay-verify="required" placeholder="请输入账号"
                        autocomplete="off" class="layui-input" readonly>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" placeholder="不修改密码请留空"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" required placeholder="请输入手机号"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" required placeholder="请输入邮箱"
                        autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-primary" id="test1">
                            <i class="layui-icon">&#xe67c;</i>选择头像
                        </button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="width:100px;height:100px;">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        var imgpath;
        layui.use(['form', 'layer', 'upload'], function () {
            var form = layui.form,
                layer = layui.layer,
                upload = layui.upload;

            // 图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: 'uploadImg'
                , field: "photo"  //默认是file
                , before: function (obj) {
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result);
                    });
                }
                , done: function (res) {
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgpath = res.data;
                    layer.msg('上传成功');
                }
                , error: function () {
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });

            // 获取URL中的id参数
            var userId = getUrlParam('id');
            if (userId) {
                // 加载用户数据
                $.ajax({
                    url: '/Sys/getUserById',
                    type: 'GET',
                    data: { id: userId },
                    success: function (res) {
                        if (res.code === 0) {
                            var user = res.data;
                            // 填充表单
                            $('input[name="id"]').val(user.id);
                            $('input[name="username"]').val(user.username);
                            $('input[name="name"]').val(user.name);
                            $('input[name="phone"]').val(user.phone);
                            $('input[name="email"]').val(user.email);
                            // 显示现有头像
                            if (user.photo) {
                                $('#demo1').attr('src', '/Sys/uploads/' + user.photo);
                                imgpath = user.photo;
                            } else {
                                $('#demo1').attr('src', '/Sys/images/defaultUser.png');
                            }
                            form.render();
                        } else {
                            layer.msg('加载用户数据失败', {icon: 2});
                        }
                    }
                });
            }

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                // 如果密码为空，删除密码字段
                if (!data.field.password) {
                    delete data.field.password;
                }
                
                // 添加头像路径
                if (imgpath) {
                    data.field.photo = imgpath;
                }
                
                $.ajax({
                    url: '/Sys/updateUser',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    success: function (res) {
                        if (res === "success") {
                            layer.msg('修改成功', {
                                icon: 1,
                                time: 2000
                            }, function(){
                                // 关闭当前frame
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                // 刷新父页面
                                parent.location.reload();
                            });
                        } else {
                            layer.msg('修改失败，请重试', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function () {
                        layer.msg('服务器错误，请联系管理员', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                return false;
            });
        });

        // 获取URL参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]); return null;
        }
    </script>
</body>
</html>